﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class ArticleCategoryOwnershipService : IArticleCategoryOwnershipService
    {
        private IArticleCategoryOwnershipManager _iar;
        public ArticleCategoryOwnershipService(IArticleCategoryOwnershipManager iar)
        {
            _iar = iar;
        }
        public async Task<int> AddAcoAsync(int bid, string fid)
        {
            return await _iar.AddAsync(new Model.ArticleCategoryOwnership 
            {
                Bid = bid,
                Fid = fid
            });
        }

        public async Task<int> DeleteAcoAsync(int id)
        {
            var data = await _iar.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _iar.DeleteAsync(data);
        }

        public async Task<List<ArticleCategoryOwnershipDto>> GetAllAcoAsync(int deleteId = 1)
        {
            var data = await _iar.GetChooseAsync(aco => aco.DeleteId == deleteId);
            return data == null ? new List<ArticleCategoryOwnershipDto>() : data.Select(aco => new ArticleCategoryOwnershipDto 
            {
                Id = aco.Id,
                Bid = aco.Bid,
                Fid = aco.Fid
            }).ToList();
        }

        public async Task<ArticleCategoryOwnershipDto> GetByBidAsync(int bid)
        {
            var data = await _iar.GetChooseAsync(b => b.Bid == bid);
            return data == null ? new ArticleCategoryOwnershipDto() : data.Select(b => new ArticleCategoryOwnershipDto
            {
                Id = b.Id,
                Bid = b.Bid,
                Fid = b.Fid
            }).First();
        }

        public async Task<ArticleCategoryOwnershipDto> GetByIdAsync(int id)
        {
            var data = await _iar.GetById(id);
            return data == null ? new ArticleCategoryOwnershipDto() : new ArticleCategoryOwnershipDto 
            {
                Id = data.Id,
                Bid = data.Bid,
                Fid = data.Fid
            };
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _iar.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _iar.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _iar.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _iar.UpdateAsync(data);
        }

        public async Task<int> UpdateAcoAsync(int id, int bid, string fid)
        {
            var data = await _iar.GetById(id);
            if (data == null)
                return -1;
            data.Bid = bid;
            data.Fid = fid;
            return await _iar.UpdateAsync(data);
        }
    }
}
